package com.ofood.data.controller;

import com.ofood.data.service.IFoodService;
import com.ofood.domain.Food;
import com.ofood.result.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * TODO:
 * 1、返回值封装
 * 2、异常处理
 * @author jack
 */
@RestController
@RequestMapping("/food")
public class FoodController {
    private Logger logger= LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IFoodService foodService;

    @RequestMapping("/get/{id}")
    public BaseResult<Food> getFoodById(@PathVariable("id") Long foodId) {
        BaseResult<Food> baseResult=new BaseResult<>(200,"sucess");

        try {
            //int a = 5 / 0;
            baseResult.setData(foodService.getFoodById(foodId));
        }catch (Exception e){
            logger.error("myerror",e);
            baseResult.setCode(500);
            baseResult.setMessage(e.getMessage());
        }

        return baseResult;
    }

    @RequestMapping("/get")
    public BaseResult<List<Food>> getAllFood(){
        BaseResult<List<Food>> baseResult=new BaseResult<>(200,"sucess");

        try {
            baseResult.setData(foodService.getAllFood());
        }catch (Exception e){
            logger.error("getAllFood",e);
            baseResult.setCode(500);
            baseResult.setMessage(e.getMessage());
        }

        return baseResult;
    }

    @RequestMapping("/post")
    public BaseResult<Boolean> insertFood(@RequestBody Food food){
        BaseResult<Boolean> baseResult=new BaseResult<>(200,"sucess");

        try {
            baseResult.setData(foodService.insertFood(food));
        }catch (Exception exception){
            logger.error("insertFood",exception);
            baseResult.setCode(500);
            baseResult.setMessage(exception.getMessage());
        }

        return baseResult;
    }

    @RequestMapping("/delete/{id}")
    public BaseResult<Boolean> deleteFood(@PathVariable("id") Long foodId){
        BaseResult<Boolean> baseResult=new BaseResult<>(200,"sucess");

        try {
            baseResult.setData(foodService.deleteFood(foodId));
        }catch (Exception e){
            logger.error("deleteFood",e);
            baseResult.setCode(500);
            baseResult.setMessage(e.getMessage());
        }

        return baseResult;
    }

    @RequestMapping("/put")
    public BaseResult<Boolean> updateFood(@RequestBody Food food){
        BaseResult<Boolean> baseResult=new BaseResult<>(200,"sucess");

        try{
            baseResult.setData(foodService.updateFood(food));
        }catch (Exception e){
            logger.error("updateFood",e);
            baseResult.setCode(500);
            baseResult.setMessage(e.getMessage());
        }

        return baseResult;
    }
}
